import constant from '../../constant';
import storage from '../../utils/storage';
import defaultUsers from '../data/users.json';

let dataName = 'users';
let data = storage.getOrDefault(dataName, defaultUsers);

export default [
    {
        url: constant.API.SYSTEM.USER.DELETE,
        type: 'post',
        response: (config) => {
            let form = config.body
            if (form) {
                let { ids } = form
                ids = ids.split(',')
                data = data.filter(e=>!ids.includes(e.id))
                storage.set(dataName, data)
            }
            return {
                'code': 1000, 'message': 'success'
            }
        }
    },
    {
        url: constant.API.SYSTEM.USER.PAGE,
        type: 'post',
        response: (config) => {
            let query = config.query
            let list = data
            if (query) {
                let { departmentIds } = query
                if (departmentIds !== "1") {
                    departmentIds = departmentIds.split(',')
                    list = list.filter(e=>departmentIds.includes(e.departmentId))
                }
            }
            return {
                'code': 1000, 'message': 'success', 'data':
                    {
                        'list': list,
                        'pagination': {
                            'page': 1,
                            'size': 20,
                            'total': list.length
                        }
                    }
            };
        }
    },
    {
        url: constant.API.SYSTEM.USER.UPDATE,
        type: 'post',
        response: (config) => {
            let user = config.body;
            if (user) {
                let us = data.filter(e => e.id === user.id);
                let currentUser = us.length > 0 ? us[0] : null;
                if (currentUser) {
                    currentUser.headImg = user.headImg;
                    currentUser.name = user.name;
                    currentUser.nickName = user.nickName;
                    currentUser.username = user.username;
                    currentUser.roleIdList = user.roleIdList;
                    currentUser.roleName = user.roleName;
                    currentUser.phone = user.phone;
                    currentUser.email = user.email;
                    currentUser.remark = user.remark;
                    currentUser.status = user.status;
                }
                storage.set(dataName, data);
            }
            return { 'code': 1000, 'message': 'success' };
        }
    }
];
